﻿using Assets.Scripts.DL.Enemy.CommonEnemy;
using Assets.Scripts.DL.Mgrs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace Assets.Scripts.DL.Enemy.FarDistanceEnemy {
	public class BowAtk : CommonState<Controller> {
		readonly Timer _cdTimer;
		bool _sendBow;
		public BowAtk(Controller controller, StateEnum stateName) : base(controller, stateName) {
			_cdTimer = _controller.StateData.AtkCdRecoder[StateName];

			AddConditionToSelf(() => _controller.IsPlayerInAtkRange()
			&& _cdTimer.Elapsed > _controller.Config.CommonAtkGap);
		}

		public override void Enter() {
			_controller.PlayAnim(StateName);
			_controller.Velocity = Vector2.zero;
			_controller.StateData.AtkOver = false;
			_sendBow = false;
		}
		public override void Update() {
			if (_controller.IsAnimPlayAt(0.65f)) {
				//生成bow
				if (!_sendBow) {
					_sendBow = true;
					//Debug.Log("MakeBow");
					var pos = _controller.transform.position;
					var off = _controller.Config.AtkOff;
					var dir = _controller.transform.localScale.x;
					var v = new Vector2(30 * dir, 0);
					PoolTools.MakeArrow(pos + (Vector3)off, v, Tags.EnemyRemoteAtk, _controller.Config.Damage);
				}
				if (_controller.IsAnimPlayOver()) {
					_controller.StateData.AtkOver = true;
				}
			}
		}

		public override void Exit() {
			_cdTimer.Reset();
			_controller.StateData.LastAtkTime.Reset();
		}

	}
}
